/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.mcx.modules.service.impl;

import com.mcx.modules.domain.PlayerItem;
import lombok.RequiredArgsConstructor;
import com.mcx.modules.repository.PlayerItemRepository;
import com.mcx.modules.service.PlayerItemService;
import com.mcx.modules.service.dto.PlayerItemDto;
import com.mcx.modules.service.dto.PlayerItemQueryCriteria;
import com.mcx.modules.service.mapstruct.PlayerItemMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.mcx.utils.PageUtil;
import com.mcx.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author mcx
* @date 2022-04-30
**/
@Service
@RequiredArgsConstructor
public class PlayerItemServiceImpl implements PlayerItemService {

    private final PlayerItemRepository playerItemRepository;
    private final PlayerItemMapper playerItemMapper;

    @Override
    public Map<String,Object> queryAll(PlayerItemQueryCriteria criteria, Pageable pageable){
        Page<PlayerItem> page = playerItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(playerItemMapper::toDto));
    }

    @Override
    public List<PlayerItemDto> queryAll(PlayerItemQueryCriteria criteria){
        return playerItemMapper.toDto(playerItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PlayerItemDto findById(Integer id) {
        PlayerItem playerItem = playerItemRepository.findById(id).orElseGet(PlayerItem::new);
        return playerItemMapper.toDto(playerItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PlayerItemDto create(PlayerItem resources) {
        return playerItemMapper.toDto(playerItemRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PlayerItem resources) {
        PlayerItem playerItem = playerItemRepository.findById(resources.getId()).orElseGet(PlayerItem::new);
        playerItem.copy(resources);
        playerItemRepository.save(playerItem);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            playerItemRepository.deleteById(id);
        }
    }
}